﻿#define Expection

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using UmcProvider.Controls;
using UmcProvider;

namespace ChinaTelecom.SmartPhone.Controls
{
#if Expection       //if reserve mode
    public interface IListViewExpection : IUmc
    {
        string Name { get; }
        bool Enabled { get; }
        Size Size { get; }
        Point Location { get; }
        string Text { get; }
        Color BackColor { get; }
        Font Font { get; }
        Color ForeColor { get; }
        System.Windows.Forms.ImageList SmallImageList { get; }
        System.Windows.Forms.ImageList LargeImageList { get; }
        System.Windows.Forms.View View { get; }
        System.Windows.Forms.ColumnHeaderStyle HeaderStyle { get; }
    }
    public interface IListView : IListViewExtension, IListViewExpection { }
    [Designer(typeof(UmcDesigner<IListViewExpection>))]
#else                  //if exclude mode
    //Add the Properties to be excluded here
    [ExcludeProperty]
    public interface IListViewExculde :  IUmc
    {
    }
    public interface IListView : IListViewExtension, IListViewExculde { }
    [Designer(typeof(UmcDesigner<ListView>))]
#endif
    [ToolboxBitmap(typeof(System.Windows.Forms.ListView), "ListView.bmp")]
    public sealed class ListView : System.Windows.Forms.ListView, IListView
    {
    }

    //Add Extension Properties here , and Implement in Inheritance class
    public interface IListViewExtension
    {
    }

    //public interface IListView : IControl
    //{
    //    System.Windows.Forms.ImageList SmallImageList { get; }
    //    System.Windows.Forms.ImageList LargeImageList { get; }
    //    System.Windows.Forms.View View { get; }
    //    System.Windows.Forms.ColumnHeaderStyle HeaderStyle { get; }
    //}
    public sealed class UmcListView : DefaultConverter<UmcListView, ListView>, IListView
    {
        public override XmlNode[] Serialize(IComponent root, XmlDocument document)
        {
            XmlNode[] nodes = base.Serialize(root, document);

            XmlNode node = nodes.First();
            //
            AppendList(document, node, root as ListView);

            return nodes;
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            IComponent component = base.DeSerialize(host, document, node);

            AddList(component as ListView, node.First());

            return component;
        }

        private void AddList(ListView listView, XmlNode node)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                listView.Items.Add(new System.Windows.Forms.ListViewItem
                {
                    Text = item.Attributes["Text"].Value,
                    ImageIndex = int.Parse(item.Attributes["ImageIndex"].Value),
                    Tag = item.Attributes["Tag"].Value
                });
            }
        }



        private void AppendList(XmlDocument document, XmlNode node, ListView listView)
        {
            if (listView.Items.Count == 0)
                return;

            foreach (System.Windows.Forms.ListViewItem lv in listView.Items)
            {
                var item = node.AppendChild(document.CreateNode(XmlNodeType.Element, "ListViewItem", null));
                item.Attributes.Append(document.CreateAttribute("Text")).Value = lv.Text;
                item.Attributes.Append(document.CreateAttribute("ImageIndex")).Value = lv.ImageIndex.ToString();
                item.Attributes.Append(document.CreateAttribute("Tag")).Value = (lv.Tag == null ? lv.Text : lv.Tag.ToString());
            }

        }



        #region IListView Members

        public System.Windows.Forms.ImageList SmallImageList
        {
            get { throw new NotImplementedException(); }
        }

        public System.Windows.Forms.ImageList LargeImageList
        {
            get { throw new NotImplementedException(); }
        }

        public System.Windows.Forms.View View
        {
            get { throw new NotImplementedException(); }
        }

        public System.Windows.Forms.ColumnHeaderStyle HeaderStyle
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IControl Members

        public bool Enabled
        {
            get { throw new NotImplementedException(); }
        }

        public Size Size
        {
            get { throw new NotImplementedException(); }
        }

        public Point Location
        {
            get { throw new NotImplementedException(); }
        }

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public Color BackColor
        {
            get { throw new NotImplementedException(); }
        }

        public Font Font
        {
            get { throw new NotImplementedException(); }
        }

        public Color ForeColor
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IUmc Members

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
